package com.software.seakdream.domain.recommend;

import com.software.seakdream.entity.AudioInfo;
import com.software.seakdream.entity.DailyAudio;
import com.software.seakdream.entity.Favorite;
import com.software.seakdream.entity.UserInfo;
import com.software.seakdream.repository.DailyAudioRepository;
import com.software.seakdream.service.FavoriteService;
import com.software.seakdream.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description:
 * @author：zzh
 * @date: 2023/5/31
 * @Copyright：
 */
@Service
public class SimilarAudios {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private DailyAudioRepository dailyAudioRepository;
    @Resource
    private FavoriteService favoriteService;

    public void setDailyAudio(){
        dailyAudioRepository.deleteAll();
        List<UserInfo> allUser = userInfoService.queryAllUser();
        for (int i = 0;i<allUser.size();i++){
            String user = allUser.get(i).getUserId();
            getSimilarAudios(user,allUser);
        }
    }

    public void getSimilarAudios(String userId,List<UserInfo> allUser){
        List<DailyAudio> myDaily = new ArrayList<>();
        //查询用户喜欢的列表的所有音乐id
        Set<String> userA = new HashSet<>();
        List<Favorite> allFavorite = favoriteService.getUserFavourite(userId);
        for (Favorite i:allFavorite){
            if (i.getIsWhite()== 1){
                userA.add("white:"+i.getWhiteId1()+"+"+i.getWhiteId2()+"+"+i.getWhiteId3());
            }else {
                userA.add(i.getAudioId());
            }
        }
        //查询所有userId

        //循环判断是传入用户的相似用户
        for (int i = 0;i<allUser.size();i++){
            if(myDaily.size()>30){
                break;
            }
            if (!allUser.get(i).getUserId().equals(userId)){
                List<Favorite> otherAllFavorite = favoriteService.getUserFavourite(allUser.get(i).getUserId());
                Set<String> userB = new HashSet<>();
                for (Favorite j:otherAllFavorite){
                    if (j.getIsWhite()== 1){
                        userB.add("white:"+j.getWhiteId1()+"+"+j.getWhiteId2()+"+"+j.getWhiteId3());
                    }else {
                        userB.add(j.getAudioId());
                    }
                }
                double similarity = calculateJaccardSimilarity(userA, userB);
                if (similarity>0.1){
                    for (Favorite j:otherAllFavorite){
                        int num = (int)(Math.random()*2);
                        if(num==1){
                            DailyAudio newOne= new DailyAudio();

                            newOne.setUserId(userId);
                            newOne.setAudioId(j.getAudioId());
                            if(j.getIsWhite()==1){
                                newOne.setIsWhite("白噪音");
                                newOne.setWhiteNoise1(j.getWhiteId1());
                                newOne.setWhiteNoise2(j.getWhiteId2());
                                newOne.setWhiteNoise3(j.getWhiteId3());
                            }
                            if(myDaily.size()>30){
                                break;
                            }
                            myDaily.add(newOne);
                            dailyAudioRepository.createDaily(newOne);
                        }

                    }
                }
            }
        }

    }

    public static double calculateJaccardSimilarity(Set<String> setA, Set<String> setB) {
        // 计算交集的大小
        Set<String> intersection = new HashSet<>(setA);
        intersection.retainAll(setB);
        int intersectionSize = intersection.size();

        // 计算并集的大小
        Set<String> union = new HashSet<>(setA);
        union.addAll(setB);
        int unionSize = union.size();

        // 计算Jaccard相似度
        double similarity = (double) intersectionSize / unionSize;

        return similarity;
    }
}
